import { OPEN_VIDEO } from '../actionTypes';
import { message } from 'antd';

export const update = (payload) => ({
  type: OPEN_VIDEO,
  payload,
});

export const changeInput = (e) => (dispatch) => {
  const val = e.target.value;
  dispatch(update({
    inputValue: e.target.value,
  }));
  if (!val) {
    dispatch(update({
      searching: false,
      searchResult: [],
    }));
  }
};

export const search = () => (dispatch, getState) => {
  const { inputValue, type } = getState().openVideo;
  const videoType = `video${type}`;
  const videoList = getState().openVideo[videoType] || [];
  if (!inputValue) {
    message.error('请输入', 1);
    return false;
  }
  console.log(inputValue);
  const res = videoList.filter(item => item.name.indexOf(inputValue) !== -1);
  dispatch(update({
    searchResult: res,
    searching: true,
  }));
};

